// pages/uploadworkForm/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    work_intro:[''],
    work_request:[''],
    salary_detail:[''],
    teamimg:'',
    imgPath:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 输入绑定事件
  inputIntro:function (e) {
    let index = e.currentTarget.dataset.index
    let work_intro = this.data.work_intro
    work_intro[index] = e.detail.value
    this.setData({
      work_intro:work_intro
    })
  },
  inputRequest:function (e) {
    let index = e.currentTarget.dataset.index
    let work_request = this.data.work_request
    work_request[index] = e.detail.value
    this.setData({
      work_request:work_request
    })
  },
  inputSalary:function (e) {
    let index = e.currentTarget.dataset.index
    let salary_detail = this.data.salary_detail
    salary_detail[index] = e.detail.value
    this.setData({
      salary_detail:salary_detail
    })
  },
  // 点击增加按钮
  addline:function () {
    let work_intro = this.data.work_intro
    work_intro.push('')
    this.setData({
      work_intro:work_intro
    })
  },
  addrequest:function () {
    let work_request = this.data.work_request
    work_request.push('')
    this.setData({
      work_request:work_request
    })
  },
  addsalary:function () {
    let salary_detail = this.data.salary_detail
    salary_detail.push('')
    this.setData({
      salary_detail:salary_detail
    })
  },
  // 点击删除按钮
  minusline:function (e) {
    let index = e.currentTarget.dataset.index
    let work_intro = this.data.work_intro
    work_intro.splice(index,1)
    this.setData({
      work_intro:work_intro
    })
  },
  minusrequest:function (e) {
    let index = e.currentTarget.dataset.index
    let work_request = this.data.work_request
    work_request.splice(index,1)
    console.log(index,work_request)
    this.setData({
      work_request:work_request
    })
  },
  minussalary:function (e) {
    let index = e.currentTarget.dataset.index
    let salary_detail = this.data.salary_detail
    salary_detail.splice(index,1)
    this.setData({
      salary_detail:salary_detail
    })
  },
  uploadTeamImg() {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        });
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let filePath = res.tempFilePaths[0];
        that.setData({
          imgPath: filePath
        })
        const name = Math.random() * 1000000;
        const cloudPath = name + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath: 'companyImgs/'+cloudPath,//云存储图片名字
          filePath,//临时路径
          success: res => {
            console.log('[上传图片] 成功：', res)
            that.setData({
              teamimg: res.fileID,//云存储图片路径,可以把这个路径存到集合，要用的时候再取出来
            });
          },
           fail: e => {
            console.error('[上传图片] 失败：', e)
          },
          complete: () => {
            wx.hideLoading()
          }
        });
      }
    })
  },
  formSubmit: function (e) {
    let data = e.detail.value
    let work_request = data.work_request.split('\n')
    let work_intro = data.work_intro.split('\n')
    let salary_detail = data.salary_detail.split('\n')
    let newIntern = Object.assign(
      data,
      {img: this.data.teamimg},
      {work_intro: work_intro},
      {salary_detail: salary_detail},
      {work_request:work_request}
      )
    
    wx.cloud.database().collection('internlist').add({
      data:newIntern,
      success:res=>{
        console.log(res)
        wx.showToast({
          title: '上传成功',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})